//
//  AddActivityPersonView.m
//  Demo
//
//  Created by MacPro on 15/12/8.
//  Copyright © 2015年 pccb. All rights reserved.
//

#import "AddActivityPersonView.h"

@interface AddActivityPersonView ()
{
    UITextField *_addPersonNameTextField;
    UITextField *_addPersonPhoneTextField;
    
    UILabel *_addPersonNumberLabel;
}

@end

@implementation AddActivityPersonView

- (instancetype)initAddActivityPersonViewWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.layer.borderColor = HexRGB(0xcecece).CGColor;
        self.layer.borderWidth = 0.5f;
        
        UIView *containerView = [[UIView alloc] init];
        containerView.backgroundColor = [UIColor whiteColor];
        containerView.frame = CGRectMake(0, 0, self.width, self.height);
        [self addSubview:containerView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 34.0f)];
        titleLabel.text = @"参加人信息";
        titleLabel.font = Default_Font_14;
        titleLabel.textColor = DefaultColor_NavigationBar;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLabel];
        
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        closeBtn.frame = CGRectMake(self.width - 37.0f, 0.0f, 34.0f, 34.0f);
        closeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [closeBtn setTitle:@"X" forState:UIControlStateNormal];
        [closeBtn setTitleColor:HexRGB(0x7d7d7d) forState:UIControlStateNormal];
        [closeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        closeBtn.backgroundColor = [UIColor clearColor];
        [closeBtn addTarget:self action:@selector(clickCloseBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:closeBtn];
        
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, titleLabel.bottom, self.width, 1.0f)];
        lineView.backgroundColor = DefaultColor_NavigationBar;
        [self addSubview:lineView];
        
        NSArray *array = @[@"联系人",@"手机号",@"人数"];
        
        for (int i = 0; i < 3; i++) {
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15.0f, titleLabel.bottom + 2.0f + 34.0f *i, 60.0f, 34.0f)];
            label.text = array[i];
            label.font = Default_Font_13;
            label.textColor = DefaultColor_NavigationBar;
            label.textAlignment = NSTextAlignmentCenter;
            [self addSubview:label];
            
            CGFloat lineTop = label.bottom;
            
            if (i < 2) {
                UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(label.right, label.top, self.width - label.right, 34.0f)];
                textField.font = Default_Font_13;
                textField.textColor = HexRGB(0x7d7d7d7);
                textField.backgroundColor = [UIColor clearColor];
                [self addSubview:textField];
                
                if (i == 0) {
                    _addPersonNameTextField = textField;
                }else{
                    textField.keyboardType = UIKeyboardTypePhonePad;
                    _addPersonPhoneTextField = textField;
                }
            }else {
                
                // 1.year month
                _addPersonNumberLabel = [[UILabel alloc] init];
                _addPersonNumberLabel.text     = [NSString stringWithFormat:@"0"];
                _addPersonNumberLabel.font     = Default_Font_13;
                _addPersonNumberLabel.textColor = HexRGB(0x7d7d7d);
                _addPersonNumberLabel.frame           = CGRectMake(label.right + 30.0f, label.top + 4.5f , self.width - label.right - 60.0f, 30.0f);
                _addPersonNumberLabel.textAlignment   = NSTextAlignmentCenter;
                _addPersonNumberLabel.backgroundColor = HexRGB(0xefefef);
                [self addSubview:_addPersonNumberLabel];
                
                
                //  左右的月份选择
                UIButton * buttonPrev       =  [UIButton buttonWithType:UIButtonTypeCustom];
                buttonPrev.frame = CGRectMake(_addPersonNumberLabel.left, _addPersonNumberLabel.top, 30.0f, 30.0f);
                [buttonPrev setTitle:@"+" forState:UIControlStateNormal];
                [buttonPrev setTitleColor:HexRGB(0x7d7d7d) forState:UIControlStateNormal];
                [buttonPrev setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
                [buttonPrev addTarget:self action:@selector(showPreviousNumber:) forControlEvents:UIControlEventTouchUpInside];
                buttonPrev.titleLabel.font          = Default_Font_15;
                buttonPrev.backgroundColor = HexRGB(0xc3c3c3);
                [self addSubview:buttonPrev];
                
                UIButton * buttonNext          = [UIButton buttonWithType:UIButtonTypeCustom];
                buttonNext.frame = CGRectMake(_addPersonNumberLabel.right - 30.0f , _addPersonNumberLabel.top, 30.0f, 30.f);
                [buttonNext setTitle:@"-" forState:UIControlStateNormal];
                [buttonNext setTitleColor:HexRGB(0x7d7d7d) forState:UIControlStateNormal];
                [buttonNext setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
                [buttonNext addTarget:self action:@selector(showNextNumber:) forControlEvents:UIControlEventTouchUpInside];
                buttonNext.titleLabel.font = Default_Font_15;
                buttonNext.backgroundColor = HexRGB(0xc3c3c3);
                [self addSubview:buttonNext];
                
                lineTop = lineTop + 5.0f;
            }
            
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, lineTop, self.width, 1.0f)];
            lineView.backgroundColor = DefaultColor_NavigationBar;
            [self addSubview:lineView];
        }
        
        UIButton * submitBtn          = [UIButton buttonWithType:UIButtonTypeCustom];
        submitBtn.frame = CGRectMake(self.width/3.0f , _addPersonNumberLabel.bottom + 15.0f, self.width / 3.0f, 30.f);
        [submitBtn setTitle:@"提 交" forState:UIControlStateNormal];
        [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [submitBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [submitBtn addTarget:self action:@selector(clickSubmitBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        submitBtn.titleLabel.font = Default_Font_15;
        submitBtn.backgroundColor = DefaultColor_NavigationBar;
        [self addSubview:submitBtn];

        frame.size.height = submitBtn.bottom + 8.0f;
        self.frame = frame;
        
    }
    
    return self;
    
}


- (void)clickCloseBtnAction:(UIButton *)sender
{
    [self removeFromSuperview];
}

- (void)showPreviousNumber:(UIButton *)sender
{
    NSInteger number = [_addPersonNumberLabel.text integerValue];
    number ++;
    
    _addPersonNumberLabel.text = [NSString stringWithFormat:@"%ld",number];
}

- (void)showNextNumber:(UIButton *)sender
{
    NSInteger number = [_addPersonNumberLabel.text integerValue];
    number --;
    if (number <= 0) {
        number = 0;
    }
    
    _addPersonNumberLabel.text = [NSString stringWithFormat:@"%ld",number];
}

- (void)clickSubmitBtnAction:(UIButton *)sender
{
    if (self.AddActivityPersonBlock) {
        NSInteger number = [_addPersonNumberLabel.text integerValue];
        self.AddActivityPersonBlock(number);
    }
}


@end
